package com.yxdj.order.message;

import com.yxdj.common.core.utils.StringUtils;
import com.yxdj.common.rabbit.constant.MqConst;
import com.yxdj.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 韩总
 */
@Component
public class RedisDelayHandler {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private RedissonClient redissonClient;

    //接收到延迟消息，取消订单
    @PostConstruct
    public void listener() {
        new Thread(()->{
            while(true) {
                try{
                    RBlockingDeque<Object> blockingDeque =
                            redissonClient.getBlockingDeque(MqConst.QUEUE_CANCEL_ORDER);
                    String orderId = (String)blockingDeque.take();
                    if(!StringUtils.isEmpty(orderId)) {
                        //调用方法取消订单
                        orderInfoService.cancelOrder(Long.parseLong(orderId));
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
